/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arthurh.android.BdeInfo.connection;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 *
 * @author arthurhalet
 */
public class ReadXML extends DefaultHandler{
    private final static String ITEM = "item";
    private final static String TITLE = "title";
    private final static String DATE = "date";
    private final static String TEXT = "text";
    private ArrayList entries;
    private boolean inItem;
    private Entry currentFeed;
    private StringBuffer buffer;
    @Override
    public void processingInstruction(String target, String data) throws SAXException {
            super.processingInstruction(target, data);
    }
    public ReadXML() {
            super();
    }
    @Override
    public void startDocument() throws SAXException {
            super.startDocument();
            entries = new ArrayList();
    }
    @Override
    public void startElement(String uri, String localName, String name,	Attributes attributes) throws SAXException {
            buffer = new StringBuffer();
            // Ci dessous, localName contient le nom du tag rencontré
            // Nous avons rencontré un tag ITEM, il faut donc instancier un nouveau feed
            if (localName.equalsIgnoreCase(ITEM)){
                    this.currentFeed = new Entry();
                    inItem = true;
            }
            
    }
    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
            if (localName.equalsIgnoreCase(TITLE)){
                    if(inItem){
                            // Les caractères sont dans l'objet buffer
                            this.currentFeed.setTitle(buffer.toString());
                            buffer = null;
                    }
            }
            if (localName.equalsIgnoreCase(DATE)){
                    if(inItem){
                            this.currentFeed.setDate(buffer.toString());
                            buffer = null;
                    }
            }
            if (localName.equalsIgnoreCase(TEXT)){
                    if(inItem){
                            this.currentFeed.setText(buffer.toString());
                            buffer = null;
                    }
            }
            
            if (localName.equalsIgnoreCase(ITEM)){
                    entries.add(currentFeed);
                    inItem = false;
            }
    }
    @Override
    public void characters(char[] ch,int start, int length)	throws SAXException{
            String lecture = new String(ch,start,length);
            if(buffer != null) buffer.append(lecture);
    }
    public ArrayList getData(){
            return entries;
    }
}
